{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/alhichri/Heartprint/blob/main/load_data_and_preprocess.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "O-NnAA8udIb0"
      },
      "source": [
        "This code load ECG data, preprocesses it, and visulaizes it"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "duqGre1kj94M"
      },
      "source": [
        "# New Section"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 36
        },
        "id": "mNy3jWcQdDno",
        "outputId": "b98f08c3-02b9-4907-dcf1-cee3033afe96"
      },
      "source": [
        "import tensorflow as tf\n",
        "import IPython.display as display\n",
        "from PIL import Image\n",
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n",
        "import os\n",
        "import pathlib\n",
        "import glob\n",
        "import scipy.io\n",
        "\n",
        "from sklearn.metrics import confusion_matrix,  classification_report, accuracy_score\n",
        "from sklearn.utils.multiclass import unique_labels\n",
        "from sklearn.utils import shuffle\n",
        "import matplotlib.image as mpimg\n",
        "from skimage import data, color\n",
        "from skimage.transform import rescale, resize, downscale_local_mean\n",
        "\n",
        "tf.__version__\n"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'2.8.0'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "kQFIfIhwdXdJ",
        "outputId": "bba789f8-71dd-419d-fd18-c8f126a35355"
      },
      "source": [
        "from google.colab import drive\n",
        "drive.mount('/content/gdrive' , force_remount=True)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Mounted at /content/gdrive\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PmAdFKb_dmoj"
      },
      "source": [
        "###################################################################################################\n",
        "########################## Parameters:  ##########################\n",
        "dataset_path = '/content/gdrive/MyDrive/000research/13-INF2168-02-R/Data-Master/Session-1/ECG/Heartbeats/'\n",
        "\n",
        "# dataset_folder = 'original_data/'; image_format = '.png'\n",
        "# images_folder = dataset_path + dataset_folder + 'images_folder/'; print(images_folder)\n",
        "\n",
        "# dataset_folder = 'original_data/'; image_format = '.png'\n",
        "# images_folder = dataset_path + dataset_folder + 'images_folder/'; print(images_folder)\n",
        "\n",
        "dataset_folder = 'Session-1'; image_format = '.gz'\n",
        "images_folder = dataset_path + dataset_folder + '/'; print(images_folder)\n",
        "\n",
        "List_of_folders = os.listdir(images_folder)\n",
        "\n",
        "print('List of folders are: ', List_of_folders)\n",
        "for x in List_of_folders:\n",
        "    print(x)\n",
        "\n",
        "num_of_classes  = len(List_of_folders)\n",
        "print('num_of_classes: ' , num_of_classes)\n",
        "\n",
        "print(images_folder+  List_of_folders[0])\n",
        "F_idx = []; list_of_files= []\n",
        "for cn in range(num_of_classes):\n",
        "    data_dir = pathlib.Path(images_folder+  List_of_folders[cn] + '/')\n",
        "    flist = list(data_dir.glob('*'+image_format)) \n",
        "    list_of_files.append(  flist   )\n",
        "\n",
        "print(list_of_files)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "mat = scipy.io.loadmat(dataset_path+'sig_s1.mat')\n",
        "# X = mat['X']; y = mat['y']; NUM_CLASSES = int(y.max()+1); print(NUM_CLASSES)\n",
        "print(mat.keys())\n",
        "h = mat['sig']\n",
        "print(h.shape)\n",
        "number_of_subjects = h.shape[1]\n",
        "for s in range(number_of_subjects):\n",
        "  h0 = h[0 , s]\n",
        "  print( s , h0.shape  )"
      ],
      "metadata": {
        "id": "9y7_sVMuWRzj"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "QRXI0KwGWxDV"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "_vA7l-wqkEJ5",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "021a9b5e-ca8d-48b8-fe9e-5ec68073caaa"
      },
      "source": [
        "dataset_path = '/content/gdrive/MyDrive/000research/13-INF2168-02-R/Data-Master/Session-1/ECG/Heartbeats/'\n",
        "\n",
        "mat = scipy.io.loadmat(dataset_path+'hb_p9_s1.mat')\n",
        "# X = mat['X']; y = mat['y']; NUM_CLASSES = int(y.max()+1); print(NUM_CLASSES)\n",
        "print(mat.keys())\n",
        "h = mat['hbs']\n",
        "print(h.shape)\n",
        "number_of_subjects = h.shape[1]\n",
        "\n",
        "# we have a problem with subject 145  h[0,145]  has shape 0,0\n",
        "X = h[0 , s] ; Y = np.zeros( ( X.shape[0],1)  ); \n",
        "label= 1\n",
        "for s in range(1,number_of_subjects):\n",
        "  h0 = h[0 , s]\n",
        "  # print( 'heartbeats: ' , h0.shape , ' label: ', s)\n",
        "  if h0.shape[0]>0:\n",
        "    X = np.vstack((X , h0))\n",
        "    labels = label * np.ones( ( h0.shape[0],1));     Y = np.vstack((Y ,  labels   ))\n",
        "    label = label + 1\n",
        "\n",
        "Y = Y.astype('int')\n",
        "print( X.shape , Y.shape)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "dict_keys(['__header__', '__version__', '__globals__', 'hbs'])\n",
            "(1, 168)\n",
            "(5653, 227) (5653, 1)\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "train_ds = tf.data.Dataset.from_tensor_slices((X, Y));     #del Y_pool, Y_pool  # to save RAM\n",
        "# dataset_generated.concatenate( train_ds )\n",
        "# print(' Two datasets are merged.')"
      ],
      "metadata": {
        "id": "wKI3VpgMUS0M"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def configure_for_performance(ds , batch_size=8, dropRemainder=True , reshuffle_each_iteration=True ):\n",
        "  ds = ds.cache()\n",
        "  ds = ds.shuffle(buffer_size=1000 , reshuffle_each_iteration=reshuffle_each_iteration)\n",
        "  ds = ds.batch(batch_size , drop_remainder=dropRemainder)\n",
        "  ds = ds.prefetch(buffer_size=tf.data.AUTOTUNE)\n",
        "  return ds\n",
        "\n",
        "train_ds_batch = configure_for_performance(train_ds , batch_size=8, dropRemainder=True )"
      ],
      "metadata": {
        "id": "6m7OUs0DaFEg"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "xVGx_P5eieog"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}